//
//  ConSingleDetailController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/4/13.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConSingleDetailController.h"
#import "ConSingleGoodsDetailCell.h"
#import "ConSingleInfoCell.h"
#import "SZLAccountHelper.h"
#import "SingleListNetWork.h"
#import "MBProgressHUD+MJ.h"
#import "SendGoodsCell.h"
#import "SingleActionTool.h"
#import "ConGotoCommentController.h"

@interface ConSingleDetailController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,strong) UIView *footView;
@property (nonatomic,strong) UIButton *footBtn;

@end

@implementation ConSingleDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    [self.view addSubview:self.sourceTab];
    [self getSingleDetail];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 193;
    }else if (indexPath.row == 1) {
        if (self.listModel.expressCompany.length>0&&self.listModel.courierNumber.length>0) {
            return 200;
        }
        return 146;
    }
    return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isSeller&&(self.listModel.status == SingleTypeStatusAlreadyPay)) {
        return 3;
    }
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ConSingleGoodsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ConSingleGoodsDetailCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.isSeller = self.isSeller;
        cell.model = self.listModel;
        return cell;
    }else if (indexPath.row == 1) {
        ConSingleInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ConSingleInfoCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.clipsToBounds = YES;
        cell.model = self.listModel;
        return cell;
    }
    SendGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SendGoodsCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.listModel;
    return cell;
}

//订单详情
- (void)getSingleDetail
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = self.listModel.orderNo;
    if (self.isSeller) {
        parame[@"type"] = @(1);
    }
    [self showClearBgWaiter];
    [SingleListNetWork singleDetailWithParame:parame response:^(BOOL success, NSString *msg, SingleListModel *model) {
        [self hideWaiter];
        if (success) {
            self.listModel = model;
            self.footBtn.tag = model.status;
            if (model.status == SingleTypeStatusAlreadyPay) {
                if (self.isSeller) {
                    [self.footBtn setTitle:@"确认发货" forState:UIControlStateNormal];
                    self.sourceTab.tableFooterView = self.footView;
                }else {
                    self.sourceTab.tableFooterView = [[UIView alloc]init];
                }
            }else if (model.status == SingleTypeStatusAlreadySend) {
                if (!self.isSeller) {
                    [self.footBtn setTitle:@"确认收货" forState:UIControlStateNormal];
                    self.sourceTab.tableFooterView = self.footView;
                }else{
                    self.sourceTab.tableFooterView = [[UIView alloc]init];
                }
            }else if (model.status == SingleTypeStatusCompleted) {
                if (!self.isSeller) {
                    [self.footBtn setTitle:@"去评价" forState:UIControlStateNormal];
                    self.sourceTab.tableFooterView = self.footView;
                }else{
                    self.sourceTab.tableFooterView = [[UIView alloc]init];
                }
            }else if (model.status == SingleTypeStatusAlreadyEvaluate) {
                self.sourceTab.tableFooterView = [[UIView alloc]init];
            }
            [self.sourceTab reloadData];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)gotoSendGoods
{
    if (self.listModel.expressCompany.length<1) {
        [MBProgressHUD showError:@"请填写快递公司"];
        return;
    }
    if (self.listModel.courierNumber.length<1) {
        [MBProgressHUD showError:@"请填写快递单号"];
        return;
    }
    [self showClearBgWaiter];
    [[SingleActionTool shareTool] sellerSendGoods:self.listModel success:^{
        [MBProgressHUD showSuccess:@"发货成功"];
        [self hideWaiter];
        [self getSingleDetail];
        if (self.successBlock) {
            self.successBlock();
        }
    }];
}

- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _sourceTab.delegate = self;
        _sourceTab.dataSource = self;
        _sourceTab.sectionFooterHeight = 0.01;
        _sourceTab.tableFooterView = [[UIView alloc]init];
        _sourceTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([ConSingleGoodsDetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ConSingleGoodsDetailCell class])];
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([ConSingleInfoCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ConSingleInfoCell class])];
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([SendGoodsCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SendGoodsCell class])];
    }
    return _sourceTab;
}
- (UIView *)footView
{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 45)];
        _footView.backgroundColor = [UIColor whiteColor];
        [_footView addSubview:self.footBtn];
    }
    return _footView;
}
- (UIButton *)footBtn
{
    if (!_footBtn) {
        _footBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-80-10, 10, 80, 30)];
        [_footBtn setTitle:@"" forState:UIControlStateNormal];
        [_footBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _footBtn.titleLabel.font = Font(15);
        _footBtn.layer.cornerRadius = 5.0;
        _footBtn.clipsToBounds = YES;
        _footBtn.layer.borderColor = [UIColor redColor].CGColor;
        _footBtn.layer.borderWidth = 1.0;
        [_footBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _footBtn;
}
- (void)btnClick:(UIButton *)btn
{
    if ([btn.titleLabel.text isEqualToString:@"确认发货"]) {
        [self gotoSendGoods];
    }else if ([btn.titleLabel.text isEqualToString:@"确认收货"]) {
        [[SingleActionTool shareTool] sureReceive:self.listModel success:^{
            [MBProgressHUD showSuccess:@"确认收货成功"];
            [self getSingleDetail];
            if (self.successBlock) {
                self.successBlock();
            }
        }];
    }else if ([btn.titleLabel.text isEqualToString:@"去评价"]) {
        ConGotoCommentController *commentVc = [[ConGotoCommentController alloc]init];
        commentVc.listModel = self.listModel;
        __WEAK
        commentVc.successBlock = ^{
            [weakSelf getSingleDetail];
            if (weakSelf.successBlock) {
                weakSelf.successBlock();
            }
        };
        [self.navigationController pushViewController:commentVc animated:YES];
    }
}

@end
